﻿//8. Write a method that adds two positive integer numbers represented as arrays of digits (each array element arr[i] contains a digit; the last digit is kept in arr[0]).
//Each of the numbers that will be added could have up to 10 000 digits.


using System;
using System.Collections.Generic;

class AddTwoNumbers
{
    static void Main()
    {
        int[] digitsFirst = {1, 4, 4, 0, 7, 7, 2};
        int[] digitsSecond = {9, 5, 6, 2, 3, 2, 7};
        Console.WriteLine(AddNumbers(digitsFirst, digitsSecond));
    }
    static string AddNumbers(int[] digitsFirst, int[] digitsSecond)
    {
        List<int> sum = new List<int>();
        int counter = 0;
        int lastTwoSum = digitsFirst[counter] + digitsSecond[counter];
        sum.Add(lastTwoSum % 10);
        sum.Add(lastTwoSum / 10);
        counter++;
        while (counter < digitsSecond.Length && counter < digitsFirst.Length)
        {
            lastTwoSum = sum[counter] + digitsFirst[counter] + digitsSecond[counter];
            sum[counter] = (lastTwoSum) % 10;
            sum.Add((lastTwoSum) / 10);
            counter++;
        }
        while (counter < digitsFirst.Length)
        {
            lastTwoSum = sum[counter] + digitsFirst[counter];
            sum[counter] = lastTwoSum % 10;
            sum.Add(lastTwoSum / 10);
            counter++;
        }
        while (counter < digitsSecond.Length)
        {
            lastTwoSum = sum[counter] + digitsSecond[counter];
            sum[counter] = lastTwoSum % 10;
            sum.Add(lastTwoSum / 10);
            counter++;
        }
        if (sum[sum.Count - 1] == 0)
        {
            sum.RemoveAt(sum.Count - 1);
        }
        char[] digitsSum = new char[sum.Count];
        for (int i = 0; i < digitsSum.Length; i++)
        {
            digitsSum[i] = Convert.ToChar(Convert.ToString(sum[sum.Count - 1 - i]));
        }
        string stringSum = new String(digitsSum);
        return stringSum;
    }
}
